﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class BookLibs : MonoBehaviour
{
	[Header("配置")]
	public Vector2Int book_size;
	public bool if_book_open = false;
	public bool if_book_center = false;
	public bool if_hide_first = false;
	public bool if_hide_last = false;

	[Header("外部传递")]
	public List<Texture2D> imgs;

	[Header("内部")]
	public BookPro bookPro;
	public AutoFlip autoFlip;
	public RectTransform next;
	public RectTransform pre;
	public GameObject[] old_paper;

	private int book_num;
	private RectTransform rect;

	private void Start()
	{
		rect = gameObject.GetComponent<RectTransform>();
	}

	/// <summary>
	/// 创建书籍
	/// </summary>
	public void Create()
	{
		//设置尺寸
		RectTransform rect = bookPro.gameObject.GetComponent<RectTransform>();
		rect.sizeDelta = book_size;
		
		//设置触控区大小
		next.sizeDelta = new Vector2(book_size.x / 2, book_size.y);
		next.anchoredPosition = new Vector2(book_size.x / 4, 0);
		pre.sizeDelta = new Vector2(book_size.x / 2, book_size.y);
		pre.anchoredPosition = new Vector2(-book_size.x / 4, 0);

		//对于页数的问题进行判定
		int book_num = imgs.Count;
		if (imgs.Count == 0)
		{
			return;
		}
		else if (imgs.Count % 2 != 0)
		{
			book_num = imgs.Count - 1;
		}

		//生成BookPro里的Papers
		bookPro.papers = GetBookPaperWithLen(book_num / 2);
		bookPro.EndFlippingPaper = book_num / 2;

		for (int i = 0; i < book_num; i++)
		{
			Create_Paper_One(i, imgs[i]);
		}

		for (int i = 0; i < old_paper.Length; i++)
		{
			old_paper[i].SetActive(false);
		}

		//是否居中
		if (if_book_center)
		{
			BookCenter();
		}

		//是否默认打开
		if (if_book_open)
		{
			Invoke("BookNext", 0.3f);
		}

	}

	/// <summary>
	/// 创建一张页面，并绑定在Paper中
	/// </summary>
	private void Create_Paper_One(int i, Texture2D img)
	{
		if (i % 2 == 0)
		{
			
			GameObject leftPage = Instantiate(bookPro.LeftPageTransform.gameObject) as GameObject;
			leftPage.transform.SetParent(bookPro.transform, true);
			leftPage.GetComponent<RectTransform>().sizeDelta = bookPro.LeftPageTransform.GetComponent<RectTransform>().sizeDelta;
			leftPage.GetComponent<RectTransform>().pivot = bookPro.LeftPageTransform.GetComponent<RectTransform>().pivot;
			leftPage.GetComponent<RectTransform>().anchoredPosition = bookPro.LeftPageTransform.GetComponent<RectTransform>().anchoredPosition;
			leftPage.GetComponent<RectTransform>().localScale = bookPro.LeftPageTransform.GetComponent<RectTransform>().localScale;
			leftPage.name = "Page" + i.ToString();
			leftPage.AddComponent<RawImage>();
			leftPage.AddComponent<Mask>().showMaskGraphic = true;
			leftPage.AddComponent<CanvasGroup>();
            RawImage p_img = leftPage.GetComponent<RawImage>();
			p_img.texture = img;

			int pi = (int)(i / 2);
			bookPro.papers[pi].Front = leftPage;
			//leftPage.SetActive(false);
		}
		else
		{
			GameObject rightPage = Instantiate(bookPro.RightPageTransform.gameObject) as GameObject;
			rightPage.transform.SetParent(bookPro.transform, true);
			rightPage.GetComponent<RectTransform>().sizeDelta = bookPro.RightPageTransform.GetComponent<RectTransform>().sizeDelta;
			rightPage.GetComponent<RectTransform>().pivot = bookPro.RightPageTransform.GetComponent<RectTransform>().pivot;
			rightPage.GetComponent<RectTransform>().anchoredPosition = bookPro.RightPageTransform.GetComponent<RectTransform>().anchoredPosition;
			rightPage.GetComponent<RectTransform>().localScale = bookPro.RightPageTransform.GetComponent<RectTransform>().localScale;
			rightPage.name = "Page" + i.ToString();
			rightPage.AddComponent<RawImage>();
			rightPage.AddComponent<Mask>().showMaskGraphic = true;
			rightPage.AddComponent<CanvasGroup>();
            RawImage p_img = rightPage.GetComponent<RawImage>();
			p_img.texture = img;

			int pi = (int)(i / 2);
			bookPro.papers[pi].Back = rightPage;
			//rightPage.SetActive(false);
		}
		
	}

	/// <summary>
	/// 重新设置Paper的长度
	/// </summary>
	/// <param name="paper"></param>
	/// <param name="len"></param>
	/// <returns></returns>
	private Paper[] GetBookPaperWithLen(int len)
	{
		Paper[] new_paper = new Paper[len];
		for (int i = 0; i < len; i++) {
			new_paper[i] = new Paper();
		}
		return new_paper;
	}

	/// <summary>
	/// 上一页
	/// </summary>
	public void BookPre()
	{
		if (if_hide_first)
		{
			if (bookPro.currentPaper <= 1)
			{
				return;
			}
		}

		autoFlip.FlipLeftPage();
		if (if_book_center)
		{
			BookCenter(1);
		}
	}

	/// <summary>
	/// 下一页
	/// </summary>
	public void BookNext()
	{
		if (if_hide_last)
		{
			if (bookPro.currentPaper >= bookPro.EndFlippingPaper - 1)
			{
				return;
			}
		}

		autoFlip.FlipRightPage();
		if (if_book_center)
		{
			BookCenter(2);
		}
	}

	/// <summary>
	/// 书本是否居中
	/// </summary>
	public void BookCenter(int p = 0)
	{
		if (bookPro.currentPaper == 0 && p == 0)
		{
			rect.DOAnchorPosX(-book_size.x / 4, 0.3f);
		}
		else if (bookPro.currentPaper == 1 && p == 1)
		{
			rect.DOAnchorPosX(-book_size.x / 4, 0.3f);
		}
		else if(bookPro.currentPaper == bookPro.EndFlippingPaper && p == 0)
		{
			rect.DOAnchorPosX(book_size.x / 4, 0.3f);
		}
		else if (bookPro.currentPaper == bookPro.EndFlippingPaper && p == 2)
		{
			rect.DOAnchorPosX(book_size.x / 4, 0.3f);
		}
		else
		{
			if (rect.anchoredPosition.x != 0)
			{
				rect.DOAnchorPosX(0, 0.3f);
			}
		}
	}

}
